#include "TextureHandler.h"

TextureHandler::~TextureHandler()
{
}

void TextureHandler::Init()
{
  glGenTextures(27, texNames);   
  
   Load2DMipMap(BUL_RES, 0);     //bullet                  0
   Load2DMipMap(CER_RES,1);     //fumo cerchio1           1
   Load2DMipMap(CER1_RES,2);    //fumo cerchio1           2
   Load2DMipMap(FUOCO_RES,3);   //fuoco missili           3
   Load2DMipMap(FUM_RES,4);     //fumo norm               4
   Load2DMipMap(FOTNORM_RES,5); //fotonorm                5
   Load2DMipMap(FOTDES_RES,6);  //puntator                6
   Load2DMipMap(FOTSIN_RES,7);  //puntatore elicot        7       
   Load2DMipMap(FOTMAL_RES,8);  //puntatore elicottero    8       
   Load2DMipMap(FOTOK_RES,9);   //puntatore elicottero    9        
   Load2DMipMap(MAP_RES,10);     //cornice mappa           10
   Load2DMipMap(POS_RES,11);     //puntatore bersaglio     11
   Load2DMipMap(POS_NEM,12);     //puntatore elicottero    12
   Load2DMipMap(SKY_RES,13);     //carico il cielo         13
   Load2DMipMap(CLOUD_RES,14);   //carico le nuvole        14
   Load2DMipMap(GRASS_RES,15);   //l'erba                  15
   Load2DMipMap(GRA_ROC_RES,16); //l'erba roc              16
   Load2DMipMap(ROUGH_RES,17);   //l'erba scura            17
   Load2DMipMap(MOUNT1_RES,18);  //la roccia               18 
   Load2DMipMap(MOUNT2_RES,19);  //la roccia               19 
   Load2DMipMap(MOUNT3_RES,20);  //la roccia               20
   Load2DMipMap(MOUNT4_RES,21);  //la roccia               21
   Load2DMipMap(MOUNT5_RES,22);  //la roccia               22
   Load2DMipMap(GROUND_RES,23);  //foto inizio             23
   Load2DMipMap(START_RES,24);   //staret                  24
   Load2DMipMap(PAUSE_RES,25);   //pausa                   25
   Load2DMipMap(END_RES,26);     //fine                    26
}


void TextureHandler::Load2DMipMap(char*  file, int num)
{
    int  iwidth, iheight;
    unsigned char *tex ;
    
    //leggo il file della texture e la genero
    tex = glmReadPPM(file, &iwidth, &iheight);
    glBindTexture(GL_TEXTURE_2D, texNames[num]);
    
    //setto i filtri
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);   
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, iwidth, iheight, 0, GL_RGBA, GL_UNSIGNED_BYTE,tex);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, iwidth, iheight, GL_RGB, GL_UNSIGNED_BYTE, tex);
    
    
    //immagazzino nom i e immagini
     texs.push_back(tex);
}

void TextureHandler::ChangeFilter()
{
  state->filter++;
  
  if (state->filter == 5) state->filter = 0;
  
  switch (state->filter)
  {
    case 0: //filtro lineare usando mipamap
            glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
            for(int i = 0;(i < 27);i++) 
            {
              glBindTexture(GL_TEXTURE_2D,texNames[i]);
              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            }
    break;
            
    case 1: //filtro lineare senza mipmap
            for(int i = 0;(i < 27);i++) 
            {
              glBindTexture(GL_TEXTURE_2D,texNames[i]);
              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            } 
    break;
    
    case 2: //filtronearest
             for(int i = 0;(i < 27);i++) 
            {
              glBindTexture(GL_TEXTURE_2D,texNames[i]);
              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            }  
    break;
    
    //modula il colre delle texture con quello presente sotto
    case 3:  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);  break;
    
    //fa il blending delle texture con il colore sottostante
    case 4:  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);  break;
    
  }
}


//cambia la modalità di visualizzazione dei poligono pieni,linee,punti
void TextureHandler::ChangePoligonMode()
{
  state->Pmode++;
  
  if (state->Pmode == 3) state->Pmode = 0;
  
  switch (state->Pmode)
  {
    case 0 : glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);break;
    case 1 : glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);break;
    case 2 : glPolygonMode(GL_FRONT_AND_BACK,GL_POINT);break;
  }
}



